home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ultra250.zip / UW_TUT6.C < prev    next >
Text File  |  1992-11-02  |  15KB  |  388 lines

  1. /****************************************************************************/
  2. /* UW_TUT6.C                                                                */
  3. /*                                                                          */
  4. /* NOTE: THIS FILE IS PUBLIC DOMAIN AND MAY BE MODIFIED AND USED AT WILL    */
  5. /*                                                                          */
  6. /* Now we add a simple menu system to allow for loading and saving of       */
  7. /* customer files.  We add three functions, file load and file save, using  */
  8. /* a simple popup window function with string entry to get a filename...    */
  9. /*                                                                          */
  10. /*                                                         Dr. Boyd Gafford */
  11. /*                                                         Kevin Huck       */
  12. /*                                                         EnQue Software   */
  13. /*                                                         09/11/92         */
  14. /****************************************************************************/
  15. #include <stdio.h>
  16. #include <fcntl.h>
  17. #include <io.h>
  18. #ifndef __TURBOC__
  19. #include <sys\types.h>
  20. #endif
  21. #include <sys\stat.h>
  22. #include <time.h>
  23. #include <ctype.h>
  24. #include "uw.h"                           /* include the necessary headers  */
  25.  
  26. #define MAX_CUST 100
  27.  
  28. typedef struct cust
  29. {
  30.   int status;
  31.   int cust_no;
  32.   char business[34];
  33.   char name[34];
  34.   char addr[34];
  35.   char city[34];
  36.   char state[4];
  37.   char zip[10];
  38.   char phone[16];
  39.   char fax[16];
  40.   char date[10];
  41.   char memo[34];
  42.   char unused[26];                                /* round out to 256 bytes */
  43. } CUST;
  44.  
  45. /*----------------------- global window variables --------------------------*/
  46. WINDOW  Desk_wn, Window1;
  47. CUST    Customers[MAX_CUST];
  48. char    Fname[33];
  49.  
  50. MENU    Top_menu, *Top_mnp = &Top_menu;
  51. MENU    Files_menu, Edit_menu;
  52. MENU    *Drop_mnps[3];
  53.  
  54. /*-------------------------------- prototypes ------------------------------*/
  55. int disp_time(void);
  56. void disp_cust(CUST *cp, WINDOW *wnp);
  57. int file_load(CUST *customers);
  58. int file_save(CUST *customers);
  59. int get_fname(char *fname);
  60.  
  61. /*********/
  62. /* ~main */
  63. /*       ********************************************************************/
  64. /*  Demonstrate data entry capability...                                    */
  65. /****************************************************************************/
  66. int main()
  67. {
  68.   int ret_val, cust = 0, end_flag = 0;
  69.   WINDOW *wnp;
  70.   CUST *cp;
  71.   uchar back_att  = (LIGHTGRAY << 4) | BLACK,
  72.         bdr_att   = (LIGHTGRAY << 4) | BLACK,
  73.         csr_att   = (CYAN << 4) | YELLOW,
  74.         first_att = (LIGHTGRAY << 4) | RED;
  75.  
  76.   wnp = &Window1;                         /* set local window pointer       */
  77.   init_video(80, 25);                     /* init video for 80 x 25 screen  */
  78.   init_clock(0x3333);                     /* init clock irq at 91 tics/sec  */
  79.  
  80.   wn_create(0, 0, V_cols-1, V_rows-1, NO_BDR, WN_NORMAL, &Desk_wn);
  81.   link_window(&Desk_wn);
  82.  
  83.   /*------------------------ create the menu system ------------------------*/
  84.   Drop_mnps[0] = &Files_menu;
  85.   Drop_mnps[1] = &Edit_menu;
  86.  
  87.   menu_create(0, 0, V_cols - 1, 0, M_HORIZONTAL,
  88.               back_att, bdr_att, csr_att, first_att,
  89.               NO_BDR, WN_NORMAL, Top_mnp);
  90.   item_add( "   Files   ", 1, 3, &Top_menu );
  91.   item_add( "   Edit    ", 2, 3, &Top_menu );
  92.  
  93.   menu_create(0, 1, 14, 5, M_VERTICAL,
  94.     back_att, bdr_att, csr_att, first_att,
  95.     SGL_BDR, WN_NORMAL, Drop_mnps[0]);
  96.   item_add( " Load File", 4, 1, &Files_menu );
  97.   item_add( " Save File", 5, 1, &Files_menu );
  98.   item_add( "   Quit   ", 3, 3, &Files_menu );
  99.  
  100.   menu_create(11, 1, 32, 4, M_VERTICAL,
  101.     back_att, bdr_att, csr_att, first_att,
  102.     SGL_BDR, WN_NORMAL, Drop_mnps[1]);
  103.   item_add( " Clear Current", 6, 7, &Edit_menu );
  104.   item_add( " Clear All    ", 7, 7, &Edit_menu );
  105.  
  106.   set_idle_func(disp_time);               /* set background clock function  */
  107.  
  108.   wn_create(5, 5, 75, 20, SLD_BDR, WN_POPUP, wnp);
  109.   wn_color(YELLOW, BLUE, wnp);            /* change the window colors       */
  110.   wn_bdr_color(WHITE, BLUE, wnp);         /* change the border's colors     */
  111.   link_window(wnp);
  112.  
  113.   /*------------- initialize first customer as EnQue Software --------------*/
  114.   cp = &Customers[0];
  115.   strcpy(cp->business, "EnQue Software"); 
  116.   strcpy(cp->name, "Kevin Huck & Boyd Gafford");  
  117.   strcpy(cp->addr, "Rt. 1 Box 116C"); 
  118.   strcpy(cp->city, "Pleasant Hill");  
  119.   strcpy(cp->state, "MO");  
  120.   strcpy(cp->zip, "64080"); 
  121.   strcpy(cp->phone, "(816)987-2515"); 
  122.   strcpy(cp->fax, "(816)987-2515");      
  123.   strcpy(cp->date, "09/11/92");      
  124.   strcpy(cp->memo, "BBS 816-358-8990"); 
  125.  
  126.   menu_set(Top_mnp);
  127.   while(!end_flag)
  128.   {
  129.     cp = &Customers[cust];
  130.     mv_cs(1,1, wnp);
  131.     wn_printf(wnp, "Customer:%3d", cust+1);
  132.     wn_plst(CENTERED, 3, "Use cursor keypad to select customer", &Desk_wn);
  133.     disp_cust(cp, wnp);
  134.     wait_event();
  135.     switch(Event.key)
  136.     {
  137.       /*------------------------- process menus ----------------------------*/
  138.       case KEY_ALT_Q:                                       /* quit program */
  139.         end_flag = 1;
  140.         break;
  141.       case KEY_ALT_F: case KEY_ALT_E:
  142.         m_show();
  143.         if( Event.key == KEY_ALT_F )
  144.           ret_val = menu_system_ll(&Top_menu,&Drop_mnps[0],0,'F',M_EXIT_ON_ESC);
  145.         else
  146.           ret_val = menu_system_ll(&Top_menu,&Drop_mnps[0],0,'E',M_EXIT_ON_ESC);
  147.         switch( ret_val )
  148.         {
  149.           case 3:                                           /* quit program */
  150.             end_flag = 1;
  151.             break;
  152.           case 4:                                           /* load file    */
  153.             file_load(Customers);
  154.             break;
  155.           case 5:                                           /* save file    */
  156.             file_save(Customers);
  157.             break;
  158.           case 6:                                           /* clear one    */
  159.             setmem(cp, sizeof(CUST), 0);
  160.             break;
  161.           case 7:                                           /* clear all    */
  162.             setmem(Customers, sizeof(Customers), 0);
  163.             break;
  164.         }
  165.         break;
  166.       /*---------------------- process cursor keys -------------------------*/
  167.       case KEY_HOME:
  168.         cust = 0;
  169.         break; 
  170.       case KEY_END:
  171.         cust = MAX_CUST-1;
  172.         break; 
  173.       case KEY_DN: 
  174.         if( cust < MAX_CUST-1 )
  175.           cust++;
  176.         break;
  177.       case KEY_UP: 
  178.         if( cust > 0 )
  179.           cust--;
  180.         break;
  181.       case KEY_PGUP: 
  182.         if( cust >= 10 )
  183.           cust -= 10;
  184.         else
  185.           cust = 0;
  186.         break;
  187.       case KEY_PGDN: 
  188.         if( cust < MAX_CUST-11 )
  189.           cust += 10;
  190.         else
  191.           cust = MAX_CUST-1;
  192.         break;
  193.       /*-------------------- process field edit keys -----------------------*/
  194.       case 'b': case 'B':                         /* get new business name  */
  195.         mv_cs( 11, 3, wnp);
  196.         wn_gets_ll(cp->business, "________________________________",
  197.                                  "********************************",
  198.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  199.         break;
  200.       case 'n': case 'N':                         /* get new contact name   */
  201.         mv_cs( 11, 4, wnp);
  202.         wn_gets_ll(cp->name, "________________________________",
  203.                              "********************************",
  204.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  205.         break;
  206.       case 'a': case 'A':                         /* get new address        */
  207.         mv_cs( 11, 5, wnp);
  208.         wn_gets_ll(cp->addr, "________________________________",
  209.                              "********************************",
  210.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  211.         break;
  212.       case 'c': case 'C':                         /* get new city           */
  213.         mv_cs( 11, 6, wnp);
  214.         wn_gets_ll(cp->city, "________________________________",
  215.                              "********************************",
  216.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  217.         break;
  218.       case 's': case 'S':                         /* get new state          */
  219.         mv_cs( 51, 6, wnp);
  220.         wn_gets_ll(cp->state, "__", "UU",
  221.           swap_nibbles(wnp->att), G_EXIT_ON_FILL|G_STRIP_END, 2, wnp);
  222.         break;
  223.       case 'z': case 'Z':                         /* get new zip            */
  224.         mv_cs( 60, 6, wnp);
  225.         wn_gets_ll(cp->zip, "_____", "#####",
  226.           swap_nibbles(wnp->att), G_EXIT_ON_FILL|G_STRIP_END, 5, wnp);
  227.         break;
  228.       case 'p': case 'P':                         /* get new phone number   */
  229.         mv_cs( 11, 7, wnp);
  230.         wn_gets_ll(cp->phone, "(___)___-____", " ### ### ####",
  231.           swap_nibbles(wnp->att), G_EXIT_ON_FILL, 14, wnp);
  232.         break;                    
  233.       case 'f': case 'F':                         /* get new fax number     */
  234.         mv_cs( 11, 8, wnp);
  235.         wn_gets_ll(cp->fax, "(___)___-____", " ### ### ####",
  236.           swap_nibbles(wnp->att), G_EXIT_ON_FILL, 14, wnp);
  237.         break;
  238.       case 'd': case 'D':                         /* get new date           */
  239.         mv_cs( 11, 9, wnp);
  240.         wn_gets_ll(cp->date, "__/__/__", "## ## ##", swap_nibbles(wnp->att),
  241.           G_EXIT_ON_FILL, 8, wnp);
  242.         break;
  243.       case 'm': case 'M':                         /* get new memo field     */
  244.         mv_cs( 11, 10, wnp);
  245.         wn_gets_ll(cp->memo, "________________________________",
  246.                              "********************************",
  247.           swap_nibbles(wnp->att), G_STRIP_END, 32, wnp);
  248.         break;
  249.  
  250.     }
  251.   }
  252.  
  253.   unlink_window(wnp);                     /* remove the window from screen  */
  254.   wn_destroy(wnp);
  255.   set_idle_func(NULL);                    /* remove background function     */
  256.   unlink_window(&Desk_wn);                /* remove the window from screen  */
  257.   wn_destroy(&Desk_wn);
  258.   end_clock();
  259.   end_video();                            /* clean up before we exit        */
  260.   return(1);
  261. }
  262. /*** end of main ***/
  263.  
  264. /**************/
  265. /* ~disp_time */
  266. /*            ***************************************************************/
  267. /*  This routine is called in the background by wait_event and will display */
  268. /*  the time once per second.  Notice the use of the global variables       */
  269. /*  Uw_timers.  There is an array of four "countdown" timers that are user  */
  270. /*  accessible.  Each "timer tic" will decrement the counts by one, until   */
  271. /*  0 is reached.  By "reloading" the timer with "Tics_per_sec", we only    */
  272. /*  display the time of day once per second.  "Tics_per_sec" is set         */
  273. /*  by init_clock.                                                          */
  274. /****************************************************************************/
  275. int disp_time(void)
  276. {
  277.   time_t t;
  278.   if( !Uw_timers[0] )                           /* has one second passed?   */
  279.   {
  280.     Uw_timers[0] = Tics_per_sec;                /* if so, reload timer      */
  281.     t = time(NULL);                             /* get time                 */
  282.     mv_cs(55, V_rows-1, &Desk_wn);              /* move window cursor       */
  283.     wn_st_qty(ctime(&t), 24, &Desk_wn);         /* output 24 characters     */
  284.     return(1);
  285.   }
  286.   return(0);
  287. }
  288. /*** end of disp_time ***/
  289.  
  290. /**************/
  291. /* ~disp_cust */
  292. /*            ***************************************************************/
  293. /*  This routine displays a customer in the desired window...               */
  294. /****************************************************************************/
  295. void disp_cust(CUST *cp, WINDOW *wnp)
  296. {
  297.   int r = 3;
  298.  
  299.   mv_cs( 1, r++, wnp );
  300.   wn_printf( wnp, "Business: %-32s", cp->business);
  301.   mv_cs( 1, r++, wnp );
  302.   wn_printf( wnp, "Name    : %-32s", cp->name );
  303.   mv_cs( 1, r++, wnp );
  304.   wn_printf( wnp, "Address : %-32s", cp->addr );
  305.   mv_cs( 1, r++, wnp );
  306.   wn_printf( wnp, "City    : %-32s State: %2s  Zip: %5s",
  307.     cp->city, cp->state, cp->zip );
  308.   wn_cleol(wnp);                                    /* clear to end of line */
  309.   mv_cs( 1, r++, wnp );
  310.   wn_printf( wnp, "Phone   : %-16s", cp->phone );
  311.   mv_cs( 1, r++, wnp );
  312.   wn_printf( wnp, "Fax     : %-16s", cp->fax );
  313.   mv_cs( 1, r++, wnp );
  314.   wn_printf( wnp, "Date    : %-10s", cp->date );
  315.   mv_cs( 1, r++, wnp );
  316.   wn_printf( wnp, "Memo    : %-32s", cp->memo );
  317. }
  318. /*** end of disp_cust ***/
  319.  
  320. /**************/
  321. /* ~file_load */
  322. /*            ***************************************************************/
  323. /*  This routine loads a file from disk into the customer array...          */
  324. /****************************************************************************/
  325. int file_load(CUST *customers)
  326. {
  327.   FILE *fp;
  328.   
  329.   if( get_fname(Fname) )
  330.   {
  331.     if( (fp = fopen(Fname, "rb")) != NULL )
  332.     {
  333.       fread(customers, sizeof(CUST), MAX_CUST, fp);
  334.       fclose(fp);
  335.       tone(1024,10);
  336.       return(1);
  337.     }
  338.   }
  339.   return(0);
  340. }
  341. /*** end of file_load ***/
  342.  
  343. /**************/
  344. /* ~file_save */
  345. /*            ***************************************************************/
  346. /*  This routine saves a file to disk from the customer array...            */
  347. /****************************************************************************/
  348. int file_save(CUST *customers)
  349. {
  350.   FILE *fp;
  351.   
  352.   if( get_fname(Fname) )
  353.   {
  354.     if( (fp = fopen(Fname, "wb")) != NULL )
  355.     {
  356.       fwrite(customers, sizeof(CUST), MAX_CUST, fp);
  357.       fclose(fp);
  358.       tone(1024,10);
  359.       return(1);
  360.     }
  361.   }
  362.   return(0);
  363. }
  364. /*** end of file_load ***/
  365.  
  366. /**************/
  367. /* ~get_fname */
  368. /*            ***************************************************************/
  369. /*  This routine prompts the user for a filename using a popup window...    */
  370. /****************************************************************************/
  371. int get_fname(char *fname)
  372. {
  373.   int ret_val = 1;
  374.   WINDOW wn;
  375.   
  376.   wn_create(20, 8, 60, 10, SLD_BDR, WN_POPUP, &wn);
  377.   wn_set(&wn);
  378.   wn_plst(4, 0, "Enter filename:", &wn);
  379.   if( wn_gets_ll(fname, "____________", "************",
  380.       swap_nibbles(wn.att), G_STRIP_END, 12, &wn) == KEY_ESC )
  381.     ret_val = 0;
  382.   wn_destroy(&wn);
  383.   return(ret_val);
  384. }
  385. /*** end of get_fname ***/
  386.  
  387. /*** END OF FILE ***/
  388.